package com.example.task;

import com.example.entity.Inventory;
import com.example.mapper.InventoryDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 1. @ClassDescription:
 * 2. @author: Z
 * 3. @date: 2024年04月15日 17:12
 */
@Slf4j
@Component
public class InventoryTask {

    @Autowired
    private InventoryDao inventoryDao;

    /**
     * @description: 处理库存过期方法
     * @time: 2024年4月15日17:14:57
     */
    @Scheduled(cron = "0 0 1 * * ?") //每天凌晨一点触发
    public void processDeliveryOrder(){
        log.info("定时处理处于过期库存：{}", LocalDateTime.now());

        LocalDateTime time = LocalDateTime.now().plusMinutes(-60);

        //查询不为2的库存
        List<Inventory> inventoryList = inventoryDao.getByStatusAndInventoryTimeLT(2,time);

        if(inventoryList!=null && inventoryList.size()>0){
            for(Inventory inventory : inventoryList){
                inventory.setStatus(2);
                inventoryDao.update(inventory);
            }
        }
    }
}
